package net.uman.web.manager;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.uman.entity.ReviewMessage;
import net.uman.framework.modules.orm.Page;
import net.uman.framework.modules.orm.PropertyFilter;
import net.uman.framework.modules.web.struts2.Struts2Utils;
import net.uman.service.ReviewMessageManager;
import net.uman.util.TextFormatter;
import net.uman.web.CrudActionSupport;

import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;
@Namespace("/manager")
public class ReviewMessageAction extends CrudActionSupport<ReviewMessage> {
	private static final long serialVersionUID = -2045186254555425321L;
	public ReviewMessage entity;
	public Long id;
	public Long articleId;
	private Page<ReviewMessage> reviewPage = new Page<ReviewMessage>(10);
	@Autowired
	private ReviewMessageManager reviewMessageManager;
	public ReviewMessage getModel() {
		return entity;
	}

	@Override
	public String list() throws Exception {
		return "list";
	}

	public String reviewMessageListData() throws Exception{
		reviewPage.setPageNo(pageNum);
		JSONObject jsonResult = new JSONObject();
		JSONArray jsonArray = new JSONArray();
		List<PropertyFilter> filters = new ArrayList<PropertyFilter>();
		PropertyFilter filter = new PropertyFilter("filter_EQL_articleId", articleId.toString());
		filters.add(filter);
		reviewMessageManager.getReviewMessagePage(reviewPage, filters);
		for(ReviewMessage reviewMessage : reviewPage.getResult()){
			JSONObject jsonNews = new JSONObject();
			jsonNews.put("id",reviewMessage.getId());
			jsonNews.put("reviewName", reviewMessage.getReviewUserName());
			jsonNews.put("reviewContent", reviewMessage.getContent());
			jsonNews.put("reviewTime", TextFormatter.formatToSimpleDate(reviewMessage.getReviewDate()));
			jsonArray.add(jsonNews);
		}
		jsonResult.put("total", reviewPage.getTotalCount());
		jsonResult.put("rows", jsonArray);
		Struts2Utils.renderJson(jsonResult);
		return null;
	}
	
	@Override
	public String input() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String save() throws Exception {
		
		return null;
	}

	@Override
	public String delete() throws Exception {
		if(id != null){
			reviewMessageManager.delete(id);
			Struts2Utils.renderText("删除成功");
		}else{
			Struts2Utils.renderText("删除失败");
		}
		return null;
	}

	@Override
	protected void prepareModel() throws Exception {
		if(id != null){
			entity = reviewMessageManager.get(id);
		}else {
			entity = new ReviewMessage();
		}
	}



}
